package com.jemmyee.pizza.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.jemmyee.core.dao.Page;
import com.jemmyee.pizza.dao.IApplyDao;
import com.jemmyee.pizza.entity.Apply;

@Repository("applyService")
public class ApplyServiceImpl implements IApplyService<Apply>{

	private static final Log log = LogFactory.getLog(ApplyServiceImpl.class);
	
	@Autowired
	private IApplyDao applyDao ;
	
	public void delete(Apply entity) {
		applyDao.delete(entity);
	}

	public void deleteAll(List<Apply> list) {
		applyDao.deleteAll(list);
		
	}

	public void deleteById(Serializable id) {
		applyDao.deleteById(id);
	}

	public List<Apply> findAll() {
		return applyDao.findAll(new Apply());
	}

	public List<Apply> findByCriteria(DetachedCriteria criteria) {
		return applyDao.findByCriteria(criteria);
	}

	public Apply findById(Serializable id) {
		return applyDao.findById(id);
	}

	public List<Apply> findByProperty(String propertyName, Object value) {
		return applyDao.findByProperty(new Apply(), propertyName, value);
	}

	public Page findPageByCriteria(DetachedCriteria detachedCriteria,
			int pageSize, int startIndex,String order) {
		return applyDao.findPageByCriteria(detachedCriteria, pageSize, startIndex, order);
	}

	public void save(Apply entity) {
		applyDao.save(entity);
	}

	public void update(Apply entity) {
		applyDao.update(entity);
	}

	public List<List<String>> queryByJDBC(String sql, int columnAmount) {
		return applyDao.queryByJDBC(sql, columnAmount);
	}
	
	
	public List<List<String>> inquiry(Integer inquiryType,Integer dateType,String startDate,String endDate){
		
		String inquirySql = "SELECT " + inquiryType(inquiryType) + ", " + dateType(dateType) + ", COUNT(*) FROM apply where 1 = 1 and startdate is not null and " + inquiryType(inquiryType) + " is not null " + dateEx(dateType(dateType),startDate,endDate) + " GROUP BY " + inquiryType(inquiryType) + ", " + dateType(dateType) ;
		
		String rowTitleSql = "select " + inquiryType(inquiryType) + " from apply where 1 = 1 and " + inquiryType(inquiryType) + " is not null " + dateEx(dateType(dateType),startDate,endDate) + " group by " + inquiryType(inquiryType) ;
		
		String colTitleSql = "select " + dateType(dateType) + " from apply where 1 = 1 and startdate is not null " + dateEx(dateType(dateType),startDate,endDate) + " group by " + dateType(dateType) ;
		
		
		List<List<String>> results = applyDao.queryByJDBC(inquirySql, 3) ;
		
		List<List<String>> rowTitles = applyDao.queryByJDBC(rowTitleSql, 1) ;
		
		List<List<String>> colTitles = applyDao.queryByJDBC(colTitleSql, 1) ;
		
		this.replaceRowTitle(inquiryType, rowTitles, results);
		
		return this.inquiry(results, rowTitles, colTitles) ;
		
	}
	
	public List<List<String>> inquiry(List<List<String>> results,List<List<String>> rowTitles,List<List<String>> colTitles){
		
	
		
		List<List<String>> viewResult = new ArrayList<List<String>>() ;
		
		List<String> tmpRow = new ArrayList<String>() ;
		tmpRow.add("") ;
		
		for(List<String> colTitle : colTitles){
			tmpRow.add(colTitle.get(0)) ;
		}
		
		viewResult.add(tmpRow) ;
		
		for(List<String> rowTitle : rowTitles){
			tmpRow = new ArrayList<String>() ;
			tmpRow.add(rowTitle.get(0)) ;
			
			for(List<String> colTitle : colTitles){
				
				String tmpValue = "" ;
				
				for(List<String> result : results){
					if(result.get(0).equals(rowTitle.get(0)) && result.get(1).equals(colTitle.get(0))){
						tmpValue = result.get(2) ;
					}
				}
				if("".equals(tmpValue))tmpValue = "0" ;
				
				tmpRow.add(tmpValue) ;
			}
			
			viewResult.add(tmpRow) ;
		}
		
		
		return viewResult ;
	}
	
	public String dateEx(String dateType,String startDate,String endDate){
		return " and " + dateType + " >= '" + startDate + "' and " + dateType + " <= '" + endDate + "' " ;
	}
	
	public String inquiryType(Integer inquiryType){
		switch(inquiryType){
		case 0 :
			return "custom_type" ;//用户类型
		case 1 :
			return "address" ;//地址区域
		case 2 :
			return "meter_type" ;//表型
		default :
			return "custom_type" ;
		}
	}
	
	public String dateType(Integer dateType){
		switch(dateType){
		case 0 :
			return "convert(varchar(4),startdate,120)" ;//年
		case 1 : 
			return "convert(varchar(7),startdate,120)" ;//月
		default :
			return "convert(varchar(4),startdate,120)" ;//年
		}
	}
	
	public void replaceRowTitle(Integer inquiryType,List<List<String>> rowTitles,List<List<String>> results){
		
		for(List<String> rowTitle : rowTitles){
			
			if(inquiryType == 0){
				if("0".equals(rowTitle.get(0))){
					rowTitle.set(0, "零散户") ;
				}
				else if("1".equals(rowTitle.get(0))){
					rowTitle.set(0, "集体户") ;
				}
				else if("2".equals(rowTitle.get(0))){
					rowTitle.set(0, "商业用户") ;
				}
			}else if(inquiryType == 2){
				if("0".equals(rowTitle.get(0))){
					rowTitle.set(0, "户内") ;
				}
				else if("1".equals(rowTitle.get(0))){
					rowTitle.set(0, "户外") ;
				}
			}
		}
		
		for(List<String> result : results){
			
			if(inquiryType == 0){
				if("0".equals(result.get(0))){
					result.set(0, "零散户") ;
				}
				else if("1".equals(result.get(0))){
					result.set(0, "集体户") ;
				}
				else if("2".equals(result.get(0))){
					result.set(0, "商业用户") ;
				}
			}else if(inquiryType == 1){
				if("0".equals(result.get(0))){
					result.set(0, "户内") ;
				}
				else if("1".equals(result.get(0))){
					result.set(0, "户外") ;
				}
			}
		}
		
	}
	
	/*
	public List<List<String>> replaceShowValue(Integer inquiryType,List<List<String>> inquiryTitle,List<List<String>> result){
		
		List<List<String>> replaceResult = new ArrayList<List<String>>() ;
		
		
		if(inquiryType == 0){
			for(List<String> value : inquiryTitle){
				List<String> newValue = new ArrayList<String>() ;
				if("0".equals(value.get(0))){
					newValue.add("零散户");
				}
				else if("1".equals(value.get(0))){
					newValue.add("集体户");
				}
				else if("2".equals(value.get(0))){
					newValue.add("商业用户");
				}
				else{
					newValue.add(value.get(0));
				}
				replaceResult.add(newValue) ;
			}
		}
		
		else if(inquiryType == 2){
			for(List<String> value : inquiryTitle){
				List<String> newValue = new ArrayList<String>() ;
				if("0".equals(value.get(0))){
					newValue.add("户内");
				}
				else if("1".equals(value.get(0))){
					newValue.add("户外");
				}
				else {
					newValue.add(value.get(0));
				}
				replaceResult.add(newValue) ;
			}
		}
		
		else {
			replaceResult = inquiryTitle ;
		}
		
		
		return replaceResult ;
	}
	*/

}
